import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:my_flutter/core/Environment.dart';
import 'package:my_flutter/store/Book.dart';
import 'package:my_flutter/store/LoginData.dart';
import 'package:my_flutter/store/Person.dart';
import 'package:my_flutter/store/Store.dart';
import 'package:my_flutter/core/Routers.dart';
import 'package:my_flutter/http/api/ApiPost.dart';
import 'package:provider/provider.dart';

///@ClassName: LoginPage
/// @Author: guoyangyang
///@Date: 2022 3/9/22 2:15 PM
///@Description: 

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<LoginPage> {
  int _counter = 0;

  TextEditingController controllerUser = TextEditingController(text: "xiaobaiyang");
  TextEditingController controllerPassword = TextEditingController(text: "123456");

  void _loginWanAndroid() async{
    Map<String, dynamic> queryParameters = Map();
    queryParameters['username'] = controllerUser.text.toString();
    queryParameters['password'] = controllerPassword.text.toString();
    //登录
    LoginData loginData = await ApiPost.loginPost(queryParameters);
    if (loginData.dioError != null) {
      //错误的时候，处理逻辑
      debugPrint(loginData.dioError?.errorMsg);
      return;
    }
    //成功的时候，处理逻辑： 保存登陆信息到store
    Store.of<LoginStore>(listen: false).setData(loginData);
  }

  void _goHomePage() {
    Routers.push(RouteName.homePage);

  }


  void _addAge() {
    Provider.of<Person>(context, listen: false).addAge();
  }

  //打开webView
  void _goWebView() {
    Routers.push(RouteName.webViewPage, {"title": "掘金首页", "url": "https://juejin.cn/user/4081023134216744/posts"});
  }

  @override
  Widget build(BuildContext context) {

    //Provider.of<Book>(context).bookName
    //Store.of<Book>().bookName
    debugPrint(Store.of<Book>().bookName);
    return Scaffold(
      appBar: AppBar(
        title: const Text("LoginPage"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("输入玩安卓用户名，密码"),
            TextField(
              controller: controllerUser,
              decoration: InputDecoration(
                  icon: Icon(Icons.account_circle, color: Colors.blue),
                hintText: "用户名",
              ),
            ),
            TextField(
              controller: controllerPassword,
              decoration: InputDecoration(
                  icon: Icon(Icons.lock_rounded, color: Colors.blue),
                  hintText: "密码"
              ),
            ),
            const SizedBox(height: 20,),
            ElevatedButton(
              onPressed: () {_loginWanAndroid();}, child: const Text("登陆"),
            ),
          ],
        ),
      ),

      floatingActionButton: FloatingActionButton(
        onPressed: _goHomePage,
        tooltip: Environment.version,
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}